package com.zhizhi.util;


import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;


public class DateUtil {

    /**
     * yyMMdd 140925
     */
    public static final String DATE_PATTERN_yyMMdd = "yyMMdd";

    /**
     * yyyyMMdd 20140518
     */
    public static String DATE_PATTERN_yyyyMMdd = "yyyyMMdd";

    /**
     * yyyy-MM-dd 2014-05-18
     */
    public static String DATE_PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
    public static String DATE_PATTERN_yyyyMMdd1 = "yyyy/MM/dd";
    public static String DATE_PATTERNyyyyMMdd = "yyyy.MM.dd";

    public static String DATE_PATTERN_yyyy = "yyyy";

    /**
     * yyyy-MM-dd HH:mm:ss 2014-05-18 20:20:20
     */
    public static String DATE_PATTERN_yyyy_MM_dd_HH_MM_ss = "yyyy-MM-dd HH:mm:ss";

    /**
     * yyyy-MM-dd HH:mm 2014-05-18 20:20
     */
    public static String DATE_PATTERN_yyyy_MM_dd_HH_MM = "yyyy-MM-dd HH:mm";
    public static String DATE_PATTERN_MM_dd_HH_MM = "MM-dd HH:mm";

    public static String DATE_PATTERN_HH_MM = "HH:mm";

    public static String DATE_PATTERN_yyyy_MM_dd_HH = "yyyy-MM-dd HH";

    public static String DATE_PATTERN_NO_SEPARATOR_YYYY_MM_DD_HH_MM = "yyyyMMddHHmmss";

    public static String DATE_PATTERN_NO_SEPARATOR_YYYY_MM_DD_HH_MM_MS = "yyyyMMddHHmmssms";

    /**
     * yyyy-MM-dd_HHmmss_SSS 2015-05-18_20-20-20_123
     */
    public static String DATE_PATTERN_yyyy_MM_dd_HHmmss_SSS = "yyyy-MM-dd_HHmmss_SSS";

    /**
     * 一天的开始时间点   00:00:00
     */
    public static String START_TIME = " 00:00:00";

    /**
     * 一天的结束时间点   23:59:59
     */
    public static String END_TIME = " 23:59:59";
    public static final int SECONDS_IN_DAY = 60 * 60 * 24;
    public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;

    public static String FIRST_DAY_OF_YEAR = "01-01";

    public static String LAST_DAY_OF_YEAR = "12-31";

    public static String CONNECTOR = "-";


    /**
     * 计算两个时间相差的月数
     *
     * @param before 开始时间
     * @param after  结束时间
     * @return 两个时间相差的月数
     */
    public static int calculateMonthCnt(Date before, Date after) {
        if (before == null) {
            throw new IllegalArgumentException("The before must not be null!");
        }

        if (after == null) {
            throw new IllegalArgumentException("The after must not be null!");
        }

        LocalDateTime beforeLocal = LocalDateTime.ofInstant(before.toInstant(), ZoneId.systemDefault());
        LocalDateTime afterLocal = LocalDateTime.ofInstant(after.toInstant(), ZoneId.systemDefault());
        Period period = Period.between(beforeLocal.toLocalDate(), afterLocal.toLocalDate());
        return period.getMonths() + period.getYears() * 12;

    }


    /**
     * 获取小时
     *
     * @return
     */
    public static int getHour() {
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.getHour();
    }

    /**
     * 获取分钟
     *
     * @return
     */
    public static int getMinute() {
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.getMinute();
    }

    /**
     * 获取当前时间是当月的第几天
     */
    public static int getDayOfMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.getDayOfMonth();

    }

    /**
     * 获取当前时间是当年的第几天
     */
    public static int getDayOfYear() {
        LocalDateTime now = LocalDateTime.now();
        return now.getDayOfYear();
    }

    /**
     * 获取月份
     *
     * @return
     */
    public static int getMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.getMonthValue();
    }

    /**
     * 获取年
     *
     * @return
     */
    public static int getYear() {
        LocalDateTime now = LocalDateTime.now();
        return now.getYear();
    }


    /**
     * 得到本月的第一天
     *
     * @return
     */
    public static String getCurrentMonthFirstDay() {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime first = localDateTime.withDayOfMonth(1);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN_yyyy_MM_dd);
        return first.format(dateTimeFormatter);
    }

    /**
     * 得到本月的最后一天
     *
     * @return
     */
    public static String getCurrentMonthLastDay() {
        LocalDateTime localDateTime = LocalDateTime.now();
        int month = localDateTime.getMonthValue();
        if (month == 12) {
            localDateTime = localDateTime.plusYears(1).withDayOfYear(1).minusDays(1);
        } else {
            localDateTime = localDateTime.plusMonths(1).withDayOfMonth(1).minusDays(1);
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN_yyyy_MM_dd);
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     * LocalDateTime  to Date
     *
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date resultDate = Date.from(instant);
        return resultDate;
    }

    /**
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return localDateTime;
    }


    /**
     * 获取季度
     *
     * @param date date=null的时候获取当前时间季度
     * @return
     */
    public static int getQuarter(Date date) {
        LocalDateTime time = null;
        if (date == null) {
            time = LocalDateTime.now();
        } else {
            time = dateToLocalDateTime(date);
        }
        int month = time.getMonthValue();
        return (month - 1) / 3 + 1;
    }

    /**
     * 获取当前周的开始时间
     *
     * @return
     */
    public static String getWeekStart() {
        LocalDateTime now = LocalDateTime.now();
        int dayOfWeek = now.getDayOfWeek().getValue() - 1;
        now = now.minusDays(dayOfWeek);
        return localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd);

    }

    /**
     * 获取当前周的结束时间
     *
     * @return
     */
    public static String getWeekEnd() {
        LocalDateTime now = LocalDateTime.now();
        int dayOfWeek = 7 - now.getDayOfWeek().getValue();
        now = now.plusDays(dayOfWeek);
        return localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd);

    }

    /**
     * 时间转字符串
     */
    public static String localDateTimeToString(LocalDateTime localDateTime, String fmt) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(fmt);
        return localDateTime.format(dateTimeFormatter);
    }


    /**
     * 获取当前季度的结束时间
     *
     * @return
     */
    public static String getCurrentQuarterEndTime() {
        int quarter = getQuarter(null);
        int endMonth = 3 * quarter;
        LocalDateTime now = LocalDateTime.now();
        if (endMonth == 12) {
            now = now.plusYears(1).withDayOfYear(1).minusDays(1);
        } else {
            now = now.withMonth(endMonth + 1).withDayOfMonth(1).minusDays(1);
        }
        return localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd);

    }

    /**
     * 获取当前季度的开始时间
     *
     * @return
     */
    public static String getCurrentQuarterStartTime() {
        int quarter = getQuarter(null);
        int startMonth = 3 * (quarter - 1) + 1;
        LocalDateTime now = LocalDateTime.now();
        now = now.withMonth(startMonth).withDayOfMonth(1);
        return localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd);

    }


    /**
     * 获取2个时间之间的所有小时时间
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getTimeList(String startTime, String endTime, String dateformate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateformate);
        LocalDateTime ldtend = LocalDateTime.parse(endTime, formatter);
        LocalDateTime ldt = LocalDateTime.parse(startTime, formatter);
        List<String> result = new ArrayList<String>();
        do {
            result.add(ldt.format(formatter));
            ldt = ldt.plusHours(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusHours(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endTime);
        }
        return result;
    }

    /**
     * 整秒数 转为时分秒
     *
     * @param time
     * @return
     */
    public static String secondToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        if (timeStr.length() < 7) {
            timeStr = "00:" + timeStr;
        }
        return timeStr;
    }

    /**
     * 格式化 时间 不足两位补 0
     *
     * @param i
     * @return
     */
    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    /**
     * 用于比较两个时间
     *
     * @param start
     * @param end
     * @param fromat
     * @return
     */
    public static long compare(String start, String end, String fromat) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fromat);
        if (fromat.length() > 11) {
            LocalDateTime ldtend = LocalDateTime.parse(end, formatter);
            LocalDateTime ldtstart = LocalDateTime.parse(start, formatter);

            return (ldtend.toEpochSecond(ZoneOffset.of("+8")) - ldtstart.toEpochSecond(ZoneOffset.of("+8")));
        } else {
            LocalDate ldtend = LocalDate.parse(end, formatter);
            LocalDate ldtstart = LocalDate.parse(start, formatter);

            return (ldtend.atStartOfDay().toEpochSecond(ZoneOffset.of("+8")) - ldtstart.atStartOfDay().toEpochSecond(ZoneOffset.of("+8")));
        }
    }

    /**
     * 用于比较两个日期
     *
     * @param startDate
     * @param endDate
     * @param fromat
     * @return
     */
    public static long compareDate(String startDate, String endDate, String fromat) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fromat);
        LocalDate ldtend = LocalDate.parse(endDate, formatter);
        LocalDate ldtstart = LocalDate.parse(startDate, formatter);
        return (ldtend.toEpochDay() - ldtstart.toEpochDay());
    }

    public static long compare(LocalDateTime start, LocalDateTime end) {

        return ((localDateTimeToDate(end).getTime() - localDateTimeToDate(start).getTime()) / 1000);
    }


    /**
     * 获取2个时间之间的所有天 且日期大于等于当前日期
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDateListUntilToday(String startTime, String endTime, String dateformate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateformate);
        LocalDate ldtend = LocalDate.parse(endTime, formatter);
        LocalDate ldtstart = LocalDate.parse(startTime, formatter);
        LocalDate now = LocalDate.now();
        List<String> result = new ArrayList<String>();
        if (ldtend.isBefore(now)) {
            return result;
        }
        if (ldtstart.isBefore(now)) {
            ldtstart = LocalDate.now();
        }
        do {
            result.add(ldtstart.format(formatter));
            ldtstart = ldtstart.plusDays(1);
        } while (ldtend.isAfter(ldtstart) || ldtend.isEqual(ldtstart));
        ldtend = ldtend.plusDays(1);
        if (!ldtend.isEqual(ldtstart)) {
            result.add(endTime);
        }
        return result;
    }


    public static byte[] str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;

        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }

        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }

        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;

        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }

            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

    /**
     * 字符串转时间
     */
    public static LocalDateTime stringToLocalDateTime(String timeStr, String fmt) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fmt);
        LocalDateTime localDateTime = LocalDateTime.parse(timeStr, formatter);
        return localDateTime;
    }

    /**
     * 根据给定的时间和格式获取时段
     */
    public static String getTimeSlot(String timeStr, String fmt) {
        LocalDateTime nowTime = DateUtil.stringToLocalDateTime(timeStr, fmt);
        LocalDateTime plusOne = nowTime.plusHours(1);
        String plusOneStrHour = DateUtil.localDateTimeToString(plusOne, "HH");
        String timeStrHour = DateUtil.localDateTimeToString(nowTime, "HH");

        return timeStrHour + ":00-" + plusOneStrHour + ":00";
    }

    /**
     * 根据给定的时间和格式 减去给定的天数
     */
    public static String minusDays(String timeStr, String fmt, int num) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fmt);
        LocalDate localDate = LocalDate.parse(timeStr, formatter);
        localDate = localDate.minusDays(num);
        return localDate.format(formatter);
    }
//
//    /**
//     * 根据给定的时间 和数字获取所有天数 排除周一周二
//     */
//    public static List<String> postpone(LocalDateTime localDateTime, int num) {
//        List<String> result = new ArrayList<>(num);
//        int weekDay = localDateTime.getDayOfWeek().getValue();
//        if (1 != weekDay && 2 != weekDay) {
//            result.add(DateUtil.localDateTimeToString(localDateTime, "yyyy-MM-dd"));
//        }
//        for (int i = 0; i < num; i++) {
//            localDateTime = localDateTime.plusDays(1);
//            String date = DateUtil.localDateTimeToString(localDateTime, "yyyy-MM-dd");
//            int dayOfWeek = localDateTime.getDayOfWeek().getValue();
//            if (1 != dayOfWeek && 2 != dayOfWeek) {
//                result.add(date);
//            }
//
//        }
//        return result;
//    }

    /**
     * 根据开始时间和结束时间以及给定的天数 计算出可预约的时间
     */
    public static List<String> postpone(String start, String end, int num, List<String> openList, List<String> closeList) {
        List<String> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        String nowStr = DateUtil.localDateTimeToString(now, "yyyy-MM-dd");
        long compare = DateUtil.compare(start + " 00:00:00", nowStr + " 00:00:00", DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);
        if (compare >= 0) {
            //已开始
            for (int i = 0; i < num; i++) {
                nowStr = DateUtil.localDateTimeToString(now, "yyyy-MM-dd");
                compare = DateUtil.compare(nowStr + " 00:00:00", end + " 00:00:00", DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);
                int dayOfWeek = now.getDayOfWeek().getValue();
                if (compare >= 0) {
                    if (dayOfWeek == 1 || dayOfWeek == 2) {
                        if (CollectionUtil.isNotEmpty(openList) && openList.contains(nowStr)) {
                            result.add(nowStr);
                        }
                    }

                    if (dayOfWeek != 1 && dayOfWeek != 2) {
                        if (CollectionUtil.isEmpty(closeList) || !closeList.contains(nowStr)) {
                            result.add(nowStr);
                        }
                    }

                }
                now = now.plusDays(1);

            }

        } else {
            //未开始

            for (int i = 0; i < num; i++) {

                nowStr = DateUtil.localDateTimeToString(now, "yyyy-MM-dd");
                compare = DateUtil.compare(nowStr + " 00:00:00", start + " 00:00:00", DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);
                if (compare <= 0) {
                    compare = DateUtil.compare(nowStr + " 00:00:00", end + " 00:00:00", DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);
                    int dayOfWeek = now.getDayOfWeek().getValue();
                    if (compare >= 0) {
                        if (dayOfWeek == 1 || dayOfWeek == 2) {
                            if (CollectionUtil.isNotEmpty(openList) && openList.contains(nowStr)) {
                                result.add(nowStr);
                            }
                        }

                        if (dayOfWeek != 1 && dayOfWeek != 2) {
                            if (CollectionUtil.isEmpty(closeList) || !closeList.contains(nowStr)) {
                                result.add(nowStr);
                            }
                        }

                    }

                }
                now = now.plusDays(1);

            }
        }
        return result;
    }


    public static List<String> postpone(LocalDateTime localDateTime, int num, List<String> openList, List<String> closeList) {
        String today = DateUtil.localDateTimeToString(localDateTime, "yyyy-MM-dd");
        List<String> result = new ArrayList<>(num);
        int weekDay = localDateTime.getDayOfWeek().getValue();
        if (1 == weekDay || 2 == weekDay) {
            if (CollectionUtil.isNotEmpty(openList) && openList.contains(today)) {
                result.add(today);
            }
        }
        if (1 != weekDay && 2 != weekDay) {
            if (CollectionUtil.isEmpty(closeList)) {
                result.add(today);
            } else if (CollectionUtil.isNotEmpty(closeList) && !closeList.contains(today)) {
                result.add(today);
            }

        }

        for (int i = 0; i < num; i++) {
            localDateTime = localDateTime.plusDays(1);
            String date = DateUtil.localDateTimeToString(localDateTime, "yyyy-MM-dd");
            int dayOfWeek = localDateTime.getDayOfWeek().getValue();
            if (1 == dayOfWeek || 2 == dayOfWeek) {
                if (CollectionUtil.isNotEmpty(openList) && openList.contains(date)) {
                    result.add(date);
                }
            }
            if (1 != dayOfWeek && 2 != dayOfWeek) {
                if (CollectionUtil.isEmpty(closeList)) {
                    result.add(date);
                } else if (CollectionUtil.isNotEmpty(closeList) && !closeList.contains(date)) {
                    result.add(date);
                }
            }

        }
        return result;
    }

    public static boolean needOpenDoor(String shangwuOrXiawu, String shangwuTime, String xiawuTime) {
        boolean flag = false;
        String time = DateUtil.localDateTimeToString(LocalDateTime.now(), DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);
        String date = time.substring(0, 10);
        String shangwu = date + " " + shangwuTime;
        String xiawu = date + " " + xiawuTime;
        if (Objects.equals("上午", shangwuOrXiawu)) {
            long copre1 = compare(time, shangwu, DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);

            long copre2 = compare(date + " 06:00:00", time, DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);

            if (copre1 > 0 && copre2 > 0) {
                flag = true;
            }

        } else {

            long copre1 = compare(shangwu, time, DateUtil.DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);

            long copre2 = compare(time, xiawu, DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);

            if (copre1 > 0 && copre2 > 0) {
                flag = true;
            }

        }
        return flag;
    }


    public static List<String> getWeekDay(LocalDateTime localDateTime, String startDate, String endDate) {
        LocalDateTime start = DateUtil.stringToLocalDateTime(startDate, "yyyy-MM-dd HH:mm:ss");
        long compare = compare(localDateTime, start);
        int dateNum = 7;
        if (compare > 0) {
            dateNum = 7;
        } else {
            LocalDateTime end = DateUtil.stringToLocalDateTime(endDate, "yyyy-MM-dd HH:mm:ss");
            long compare1 = compare(localDateTime, end);
            if (compare1 > 0) {
                dateNum = 10;
            }

        }

        List<String> result = new ArrayList<>();
        int dayOfWeek = localDateTime.getDayOfWeek().getValue();
        int firstDay = 1 - dayOfWeek;
        localDateTime = localDateTime.plusDays(firstDay);


        for (int i = 0; i < dateNum; i++) {
            String date = DateUtil.localDateTimeToString(localDateTime.plusDays(i), "yyyy-MM-dd");
            result.add(date);
            firstDay++;
        }
        return result;
    }

    /**
     * @param
     * @return java.util.List<java.lang.String>
     * @Description:获取周的所有天数
     * @menu /
     * @status done
     */
    public static List<String> getWeekDay() {
        LocalDateTime now = LocalDateTime.now();
        List<String> result = new ArrayList<>();
        int dayOfWeek = now.getDayOfWeek().getValue();
        int firstDay = 1 - dayOfWeek;
        now = now.plusDays(firstDay);
        for (int i = 0; i < 7; i++) {
            String date = DateUtil.localDateTimeToString(now.plusDays(i), "yyyy-MM-dd");
            result.add(date);
            firstDay++;
        }
        return result;
    }


    public static List<String> getMonthDay() {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int num = year % 4;
        int monthDay = now.getMonth().length(false);
        if (num == 0) {
            monthDay = now.getMonth().length(true);
        }
        List<String> result = new ArrayList<>();
        int dayOfMonth = now.getDayOfMonth();
        int firstDay = 1 - dayOfMonth;
        now = now.plusDays(firstDay);

        for (int i = 0; i < monthDay; i++) {
            String date = DateUtil.localDateTimeToString(now.plusDays(i), "yyyy-MM-dd");
            result.add(date);
            firstDay++;
        }
        return result;
    }


    /**
     * 获取2个时间之间的所有天
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDateList(String startTime, String endTime, String dateformate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateformate);
        LocalDate ldtend = LocalDate.parse(endTime, formatter);
        LocalDate ldt = LocalDate.parse(startTime, formatter);
        List<String> result = new ArrayList<String>();
        do {
            result.add(ldt.format(formatter));
            ldt = ldt.plusDays(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusDays(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endTime);
        }
        return result;
    }


    /**
     * 获取开始时间上周的每一天
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getLastWeekDateList(String startTime, String endTime, String dateformate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateformate);
        LocalDate ldtend = LocalDate.parse(endTime, formatter).plusDays(-7);
        LocalDate ldt = LocalDate.parse(startTime, formatter).plusDays(-7);
        List<String> result = new ArrayList<String>();
        do {
            result.add(ldt.format(formatter));
            ldt = ldt.plusDays(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusDays(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endTime);
        }
        return result;
    }

    /***
     * @Description String 类的时间转换成date类型
     * @param dstr:
     * @param format:
     */
    public static Date getStringToDate(String dstr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(dstr);
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            return date;
        }
    }

    /***
     * @Description 获取yyyy-MM-dd 格式的时间
     * @return java.util.Date
     */
    public static Date getNowDate(String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = new Date();
            String dateStr = sdf.format(date);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * @Description Date转String类型
     * @return java.util.Date
     */
    public static String getDateToStr(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateStr = sdf.format(date);
        return dateStr;
    }


    /**
     * 获取开始时间上月的每一天 排除周一周二
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getLastMonthDateListNoMonTue(String startTime, String endTime, String dateformate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateformate);
        LocalDate ldtend = LocalDate.parse(endTime, formatter).withDayOfMonth(1).plusDays(-1);
        LocalDate ldt = LocalDate.parse(startTime, formatter).plusMonths(-1);
        List<String> result = new ArrayList<String>();
        do {
//            if (ldt.getDayOfWeek().getValue() != 1 && ldt.getDayOfWeek().getValue() != 2) {
            result.add(ldt.format(formatter));
//            }

            ldt = ldt.plusDays(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusDays(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endTime);
        }
        return result;
    }

    public static List<String> getMothList(String startMonth, String endMonth) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate ldtend = LocalDate.parse(endMonth + "-01", formatter);
        LocalDate ldt = LocalDate.parse(startMonth + "-01", formatter);
        List<String> result = new ArrayList<String>();
        do {

            result.add(ldt.format(formatter));

            ldt = ldt.plusMonths(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusMonths(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endMonth);
        }
        List<String> finalResult = new ArrayList<>();
        result.forEach(i -> {
            finalResult.add(i.substring(0, 7));
        });
        return finalResult;
    }

    public static List<String> getLastYearMonthList(String startMonth, String endMonth) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate ldtend = LocalDate.parse(endMonth + "-01", formatter).plusYears(-1);
        LocalDate ldt = LocalDate.parse(startMonth + "-01", formatter).plusYears(-1);
        List<String> result = new ArrayList<String>();
        do {

            result.add(ldt.format(formatter));

            ldt = ldt.plusMonths(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusMonths(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endMonth);
        }
        List<String> finalResult = new ArrayList<>();
        result.forEach(i -> {
            finalResult.add(i.substring(0, 7));
        });
        return finalResult;
    }

    public static String getLastDayByMonth(String yearAndMonth) {
        LocalDateTime localDateTime = DateUtil.stringToLocalDateTime(yearAndMonth + "-01 00:00:00", DATE_PATTERN_yyyy_MM_dd_HH_MM_ss);
        int month = localDateTime.getMonthValue();
        if (month == 12) {
            localDateTime = localDateTime.plusYears(1).withDayOfYear(1).minusDays(1);
        } else {
            localDateTime = localDateTime.plusMonths(1).withDayOfMonth(1).minusDays(1);
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN_yyyy_MM_dd);
        return localDateTime.format(dateTimeFormatter);
    }

    public static List<String> getYearList(String startYear, String endYear) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate ldtend = LocalDate.parse(endYear + "-01-01", formatter);
        LocalDate ldt = LocalDate.parse(startYear + "-01-01", formatter);
        List<String> result = new ArrayList<String>();
        do {
            result.add(ldt.format(formatter));
            ldt = ldt.plusYears(1);
        } while (ldtend.isAfter(ldt) || ldtend.isEqual(ldt));
        ldtend = ldtend.plusYears(1);
        if (!ldtend.isEqual(ldt)) {
            result.add(endYear);
        }
        List<String> finalResult = new ArrayList<>();
        result.forEach(i -> {
            finalResult.add(i.substring(0, 4));
        });
        return finalResult;
    }

    /**
     * @param
     * @return java.util.List<java.lang.String>
     * @Description: 获取近7天
     * @menu /
     * @status done
     */
    public static List<String> getNearest7day() {
        List<String> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        now = now.plusDays(-7);
        for (int i = 0; i < 7; i++) {
            now = now.plusDays(1);
            result.add(DateUtil.localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd));
        }
        return result;
    }

    public static List<List<String>> getWeeksInMonth(List<String> dateList) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtil.DATE_PATTERN_yyyy_MM_dd);
        List<List<String>> result = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            List<String> week = new ArrayList<>();
            ListIterator<String> li = dateList.listIterator();
            while (li.hasNext()) {
                String date = li.next();
                if (CollectionUtil.isEmpty(week)) {
                    LocalDate ldt = LocalDate.parse(date, formatter);
                    int dateNum = ldt.getDayOfWeek().getValue();
                    int size = 8 - dateNum;
                    week.add(size + "," + date);
                    li.remove();
                } else {
                    int size = Integer.parseInt(week.get(0).split(",")[0]);
                    if (week.size() > size - 1) {
                        result.add(week);
                        break;
                    } else {
                        week.add(date);
                        li.remove();
                        if (CollectionUtil.isEmpty(dateList)) {
                            result.add(week);
                        }
                    }
                }

            }
        }
        List<List<String>> finalReturn = new ArrayList<>();
        result.forEach(i -> {
            List<String> week = new ArrayList<>();
            i.forEach(j -> {
                if (j.contains(",")) {
                    week.add(j.split(",")[1]);
                } else {
                    week.add(j);
                }
            });
            finalReturn.add(week);
        });


        return finalReturn;
    }

    /**
     * @param startTime, endTime
     * @return int
     * @Description: 开始时间和结束时间获取工期
     * @menu /
     * @status done
     */
    public static int getDuration(String startTime, String endTime) {
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            return 0;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN_yyyy_MM_dd);
        LocalDate ldtend = LocalDate.parse(endTime, formatter);
        LocalDate ldtstart = LocalDate.parse(startTime, formatter);
        AtomicInteger result = new AtomicInteger(0);
        while (!ldtstart.isEqual(ldtend)) {
            ldtstart = ldtstart.plusDays(1);
            result.addAndGet(1);
        }
        return result.get() + 1;
    }

    /**
     * @param dateNum, history  向历史或向未来数的天数  true  向历史
     * @return java.util.List<java.lang.String>
     * @Description:
     * @menu /
     * @status done
     */
    public static List<String> getDateListHistoryOrFuture(int dateNum, boolean history) {
        List<String> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        if (history) {
            now = now.plusDays(0 - dateNum);
            for (int i = 0; i < dateNum; i++) {
                now = now.plusDays(1);
                result.add(DateUtil.localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd));
            }
        } else {
            for (int i = 0; i < dateNum; i++) {
                now = now.plusDays(1);
                result.add(DateUtil.localDateTimeToString(now, DATE_PATTERN_yyyy_MM_dd));
            }

        }


        return result;
    }


    public static void main(String[] args) throws Exception {

        System.out.println(compare("2020-10-14","2020-10-14","yyyy-MM-dd"));
//        LocalDateTime localDateTime = LocalDateTime.now();
//        getDateList("2020-09-01", "2020-09-31", "yyyy-MM-dd").forEach(i -> {
//            System.out.println(i);
//        });
//        DateUtil.getDateListNoMonTue("2020-05-15", "2020-05-29", DateUtil.DATE_PATTERN_yyyy_MM_dd).forEach(i -> {
//            System.out.println(i);
//        });
//        NumberFormat nt = NumberFormat.getPercentInstance();
//        System.out.println(nt.format(-2));


//        List<VisitBehavior> list = new ArrayList<>();
//        list.add(VisitBehavior.builder().totalNum(12).build());
//        list.add(VisitBehavior.builder().totalNum(13).build());
//        list.add(VisitBehavior.builder().totalNum(14).build());
//        list.add(VisitBehavior.builder().totalNum(16).build());
//        CollectionUtil.sort(list, new Comparator<VisitBehavior>() {
//            @Override
//            public int compare(VisitBehavior o1, VisitBehavior o2) {
//                return (int) (o2.getTotalNum() - o1.getTotalNum());
//            }
//        });
//        list.subList(0, 3).forEach(a -> {
//            System.out.println(a.getTotalNum());
//        });

//        Map<String,Object> dataMap = new HashMap();
//        List<PassengerFlow> list = new ArrayList<PassengerFlow>();
//        for(int i=0;i<10;i++){
//            PassengerFlow flow = new PassengerFlow();
//            flow.setInCnt(100*i+100);
//            flow.setSurplusCnt(86*(i+1));
//            flow.setTimeSlot(i+11+"时间段");
//            list.add(flow);
//        }
//        dataMap.put("flowList", list);
//        dataMap.put("a","aa");
//        dataMap.put("b","BB");
//        dataMap.put("c",100);
//        dataMap.put("d",1100);
//
//
//        Configuration configuration = new Configuration();
//        configuration.setDefaultEncoding("utf-8");
//        configuration.setDirectoryForTemplateLoading(new File("d:/"));
//        File outFile = new File("D:/test.doc");
//        Template t =  configuration.getTemplate("word测试.ftl","utf-8");
//        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
//        t.process(dataMap, out);
//        out.close();

//        postpone(localDateTime,2).forEach( i->{
//            System.out.println(i);
//        });

//        localDateTime = localDateTime.plusDays(-7);
//        System.out.println(DateUtil.localDateTimeToString(localDateTime,"yyyy-MM-dd"));


//        getWeekDay(localDateTime, "2020-01-24 00:00:00","2020-01-26 23:59:59").forEach(i -> {
//            System.out.println(i);
//        });

//        String startDate = "2020-02-01";
//        String endDate = "2020-03-31";
//        int du = getDuration(startDate, endDate);
//        System.out.println(du);

//        List<String> currentDateList = DateUtil.getDateListNoMonTue(startDate, endDate, DateUtil.DATE_PATTERN_yyyy_MM_dd);
//        List<String> lastDateList = new ArrayList<>();
//        if (currentDateList.size() < 8) {
//            //上个周的
//            lastDateList = DateUtil.getLastWeekDateListNoMonTue(startDate, endDate, DateUtil.DATE_PATTERN_yyyy_MM_dd);
//        } else {
//            // 上个月的
//            lastDateList = DateUtil.getLastMonthDateListNoMonTue(startDate, endDate, DateUtil.DATE_PATTERN_yyyy_MM_dd);
//        }
//
//        currentDateList.forEach(i -> {
//
//            System.out.println(i);
//
//
//        });
    }


}
